
namespace AdminSG3L.Domain.Entities;

/// <summary>
/// 角色实体。
/// </summary>
public class AppRole : EntityBase
    {
        /// <summary>
        /// 角色名称，必填。
        /// </summary>
        public string RoleName { get; private set; }
        /// <summary>
        /// 角色编码，可选。
        /// </summary>
        public string? RoleCode { get; private set; }

        /// <summary>
        /// 拥有该角色的用户集合。
        /// </summary>
        public ICollection<AppUser> Users { get; private set; } = [];
        /// <summary>
        /// 角色拥有的权限集合。
        /// </summary>
        public ICollection<AppPermission> Permissions { get; private set; } = [];
        /// <summary>
        /// 角色拥有的菜单集合。
        /// </summary>
        public ICollection<AppMenu> Menus { get; private set; } = [];

        /// <summary>
        /// 私有构造函数，仅限工厂方法调用。
        /// </summary>
        /// <param name="roleName">角色名称。</param>
        /// <param name="roleCode">角色编码。</param>
        /// <param name="description">描述。</param>
        private AppRole(string roleName, string? roleCode,string? description)
        {
            RoleName = roleName;
            RoleCode = roleCode;
            Description = description;
        }

    /// <summary>
    /// 创建 AppRole 对象的工厂方法。
    /// </summary>
    /// <param name="roleName">角色名称。</param>
    /// <param name="roleCode">角色编码。</param>
    /// <param name="description">描述。</param>
    /// <param name="isSystem">是否为系统角色。</param>
    /// <returns>AppRole 对象。</returns>
    public static AppRole Create(string roleName, string? roleCode = null, string? description = null, bool isSystem = false)
    {
        var role = new AppRole(roleName, roleCode, description);
        role.IsSystem = isSystem;
        return role;
    }        /// <summary>
        /// </summary>
        /// <param name="permission">要添加的权限。</param>
        public void AddPermission(AppPermission permission)
        {
            if (!Permissions.Contains(permission))
            {
                Permissions.Add(permission);
            }
        }

        /// <summary>
        /// 移除角色权限。
        /// </summary>
        /// <param name="permission">要移除的权限。</param>
        public void RemovePermission(AppPermission permission)
        {
            Permissions.Remove(permission);
        }

        /// <summary>
        /// 给角色分配菜单。
        /// </summary>
        /// <param name="menu">要添加的菜单。</param>
        public void AddMenu(AppMenu menu)
        {
            if (!Menus.Contains(menu))
            {
                Menus.Add(menu);
            }
        }

        /// <summary>
        /// 移除角色菜单。
        /// </summary>
        /// <param name="menu">要移除的菜单。</param>
        public void RemoveMenu(AppMenu menu)
        {
            if (Menus.Contains(menu))
            {
                Menus.Remove(menu);
            }
        }

        public void Update(string roleName, string? roleCode, string? description)
        {
            if (IsSystem)
            {
                throw new InvalidOperationException("系统角色不允许修改");
            }
            RoleName = roleName;
            RoleCode = roleCode;
            Description = description;
        }
    }